package com.yunhe.abnormal.controller.base;

import com.yunhe.common.model.DomainList;
import com.yunhe.abnormal.domain.base.AbnormalStatus;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.base.AbnormalStatusDTO;
import com.yunhe.abnormal.service.base.AbnormalStatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Api(value = "abnormalStatus",description = "异常处理状态维护", tags = {"Base_AbnormalStatus"})
@RestController
@RequestMapping("/abnormalStatuses")
public class AbnormalStatusController {

    @Autowired
    private AbnormalStatusService abnormalStatusService;

    /**
     * 新增异常状态
     * @param deviceAnomalyType 异常状态对象
     */
    @ApiOperation("新增异常状态")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@RequestBody AbnormalStatus deviceAnomalyType)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        AbnormalStatusDTO newDeviceAnomalyType = abnormalStatusService.createAbnormalStatus(deviceAnomalyType);
        return new ResponseEntity<>(new ResultObject<>(newDeviceAnomalyType), HttpStatus.OK);
    }

    /**
     * 批量新增异常状态
     * @param domainList 异常状态对象列表
     */
    @ApiOperation("批量新增异常状态")
    @RequestMapping(value = "/batch", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@RequestBody DomainList<AbnormalStatus> domainList)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        Iterable<AbnormalStatusDTO> results = abnormalStatusService.createAbnormalStatus(domainList.getList());
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 根据ID删除异常状态
     * @param id 异常状态ID
     */
    @ApiOperation("根据ID删除异常状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常状态ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
        abnormalStatusService.deleteAbnormalStatus(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 更新异常状态
     * @param id 异常状态ID
     * @param abnormalType 异常状态对象
     */
    @ApiOperation("更新异常状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常状态ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> put(@PathVariable("id") Long id,
                                            @RequestBody AbnormalStatus abnormalType)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        abnormalType.setId(id);
        AbnormalStatusDTO newDeviceAnomalyType = abnormalStatusService.updateAbnormalStatus(abnormalType);
        return new ResponseEntity<>(new ResultObject<>(newDeviceAnomalyType), HttpStatus.OK);
    }

    /**
     * 更新异常状态
     * @param id 异常状态ID
     * @param code 异常状态代码
     * @param name 异常状态编号
     * @param title 异常状态名称
     * @param sn 排序序号
     * @param activity 是否可用
     */
    @ApiOperation("更新异常状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常状态ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "code", value = "异常状态代码", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "异常状态编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "title", value = "异常状态名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sn", value = "排序序号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "activity", value = "是否可用", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public ResponseEntity<ResultObject> patch(@PathVariable("id") Long id,
                                              @RequestParam("code") Optional<Long> code,
                                              @RequestParam("name") Optional<String> name,
                                              @RequestParam("title") Optional<String> title,
                                              @RequestParam("sn") Optional<Long> sn,
                                              @RequestParam("activity") Optional<Boolean> activity)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AbnormalStatusDTO newDeviceAnomalyType = abnormalStatusService.updateAbnormalStatus(id, code, name, title, sn, activity);
        return new ResponseEntity<>(new ResultObject<>(newDeviceAnomalyType), HttpStatus.OK);
    }

    /**
     * 根据异常状态ID获得异常状态
     * @param id 异常状态ID
     */
    @ApiOperation("根据异常状态ID获得异常状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常状态ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id) {
        AbnormalStatusDTO DeviceAnomalyType = abnormalStatusService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(DeviceAnomalyType), HttpStatus.OK);
    }

    /**
     * 根据条件获得异常状态列表
     */
    @ApiOperation("根据条件获得异常状态列表")
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search() {
        Iterable<AbnormalStatusDTO> results = abnormalStatusService.findAll();
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    /**
     * 绑定异常名称
     * @return
     */
    @ApiOperation("绑定异常名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "异常状态代码", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "statusCodes", value = "异常名称代码", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/bind", method = RequestMethod.PUT)
    public ResponseEntity<ResultObject> bindAlarmStatus(@RequestParam("code") Long code,
                                                        @RequestParam("statusCodes") List<Long> statusCodes) {
        abnormalStatusService.bindAlarmStatus(code, statusCodes);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    @ApiOperation("根据name查找异常处理状态")
    @RequestMapping(value = "/byName", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findByName(@RequestParam("name") String name)
            throws ArgumentErrorException, UniqueConstraintsException, ObjectNotFoundException {
        AbnormalStatusDTO abnormalStatus = abnormalStatusService.findByName(name);
        return new ResponseEntity<>(new ResultObject<>(abnormalStatus), HttpStatus.OK);
    }
}
